#include <iostream>
#include "httplib.h"
#include "contacts.pb.h"
#include "Exception.h"
#include <exception>

using namespace httplib;
void menu()
{
    std::cout << "-----------------------------------------------------" << std::endl
              << "--------------- 请选择对通讯录的操作 ----------------" << std::endl
              << "------------------ 1、新增联系⼈ --------------------" << std::endl
              << "------------------ 2、删除联系⼈ --------------------" << std::endl
              << "------------------ 3、查看联系⼈列表 ----------------" << std::endl
              << "------------------ 4、查看联系⼈详细信息 ------------" << std::endl
              << "------------------ 0、退出 --------------------------" << std::endl
              << "-----------------------------------------------------" << std::endl;
}
void addcontacts(add_contacts::AddContactsRequest& req)
{
    std::cout<<"-------------新增联系⼈-------------"<<std::endl;
    std::cout<<"请输⼊联系⼈姓名: ";
    std::string name;
    getline(std::cin,name);
    req.set_name(name);
    std::cout<<"请输⼊联系⼈年龄: ";
    int age;
    std::cin>>age;
    req.set_age(age);
    std::cin.ignore(256,'\n');//清空缓冲区的\n
    
    for(int i = 0;;i++)
    {
        std::cout<<"请输⼊联系⼈电话"<<i+1<<": ";
        std::string number;
        getline(std::cin,number);
        if(number=="")
            break; 
        add_contacts::AddContactsRequest_Phone* phone = req.add_phone();
        phone->set_number(number);
        std::cout<<"请输入电话类型(1.移动电话 2.固定电话): ";
        int type;
        std::cin>>type;
        std::cin.ignore(256,'\n');
        switch(type)
        {
            case 1:
                phone->set_type(::add_contacts::AddContactsRequest_Phone_PhoneType::AddContactsRequest_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(::add_contacts::AddContactsRequest_Phone_PhoneType::AddContactsRequest_Phone_PhoneType_TEL);
                break;
            default:
                std::cout<<"输入错误"<<std::endl;
                break;
        }

    }
}
void AddContacts()
{

    add_contacts::AddContactsRequest req;
    //填写联系人信息
    addcontacts(req);
    //序列化
    std::string req_str;
    if(!req.SerializeToString(&req_str))
    {
        Exception e("序列化失败");
        throw e;
    }
    //发送
    Client client("localhost",8080);
    auto result = client.Post("/contacts/add",req_str,"application/protobuf");
    if(!result)
    {
        Exception e("获取连接失败");
        throw e;
    }
    //反序列化
    add_contacts::AddContactsResponse resp;
    bool prase = resp.ParseFromString(result->body);
    if(!prase&&result->status!=200)
    {
        std::string err = "/contacts/add调用失败("+resp.error_desc()+")";
        throw Exception(err);
    }
    else if(result->status!=200)
    {
        std::string err = "/contacts/add调用失败("+resp.error_desc()+")";
        throw Exception(err);
    }
    else if(!resp.success())
    {
        std::string err = "/contacts/add异常("+resp.error_desc()+")";
        throw Exception(err);
    }

    std::cout<<"新增联系人成功,UID: "<<resp.uid()<<std::endl;

}
int main()
{
    enum OPTION{ QUIT = 0,ADD,DEL,FIND_ALL,FIND_ONE};
    while (true)
    {
        menu();
        std::cout<<"---->请输入: ";
        int choose;
        std::cin>>choose;
        std::cin.ignore(256,'\n');
       
            switch(choose)
            {
                case ADD:
                    try{
                        AddContacts();
                    }catch(Exception e)
                    {
                        std::cout<<"发生异常: "<<e.what()<<std::endl;
                    }
                    break;
                case QUIT:
                    std::cout<<"---->退出程序"<<std::endl;
                    return 0;
                case DEL:
                    break;
                case FIND_ALL:
                    break;
                case FIND_ONE:
                    break;
                default:
                    std::cout<<"选择错误，请重新选择"<<std::endl;
                    break;
            }     
        
    }

    return 0;
}

// int main() {
//     httplib::Client cli("localhost", 8080);
 
//     // 发送GET请求
//     auto res_get = cli.Get("/hello");
//     if (res_get && res_get->status == 200) {
//         std::cout << "GET response: " << res_get->body << std::endl;
//     } else {
//         std::cerr << "GET request failed!" << std::endl;
//     }
 
//     // 发送POST请求
//     httplib::MultipartFormDataItems items = {
//         {"message", "Hello from client!"}
//     };
//     auto res_post = cli.Post("/post", items);
//     if (res_post && res_post->status == 200) {
//         std::cout << "POST response: " << res_post->body << std::endl;
//     } else {
//         std::cerr << "POST request failed!" << std::endl;
//     }
 
//     return 0;
// }